// home//pages/search/index.js
const apis = require('../../../utils/api').apis;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hotSearch: [],
    searchWord: null,
    searchArr: [],
    show: false,
    searchResultArr: [],
    labelArr: [],
    searchShow: 1,
    switchShow: false,
    searchMore: [],
    searchMoreNo: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    wx.showLoading({
      title: '加载中',
    })
    apis.searchQueryHotSearch({}).then(res => {
      _this.setData({
        hotSearch: res.data
      })
      wx.hideLoading()
    })
    let searchArr = _this.data.searchArr;
    searchArr = wx.getStorageSync('searchArr')
    // console.log(searchArr)
    this.setData({
      searchArr
    })

  },
  inputFoucs(e) {
    let _this = this;
    let val = e.detail.value;
    if (val == '') {
      _this.setData({
        show: false,
        switchShow: false
      })
      let searchArr = _this.data.searchArr;
      searchArr = wx.getStorageSync('searchArr')
      this.setData({
        searchArr
      })
    }
  },

  bindconfirm(e) {
    let _this = this;
    let val = e.detail.value;
    console.log(val)
    this.setData({
      searchWord:val
    })
    if (val != '') {
      let val = e.detail.value;
      var value = wx.getStorageSync('searchArr') || [] //声明plates
      value.unshift(val) //添加
      let searchArr = [];
      value = value.reduce(function (x, y) {
        searchArr[y] ? '' : searchArr[y] = true && x.push(y);
        return x;
      }, []);
      wx.setStorageSync('searchArr', value) //同步数据存储
      apis.searchSearchQuery({
        searchWhere: val
      }).then(res => {
        // console.log(res)

        if (res.data.news.records) {
          res.data.news.records.map(item => {
            item.label = item.label.split(',');
            console.log(item.label)

          })
        }
        if (res.data.recruitment.records) {
          res.data.recruitment.records.map(item => {
            item.experienceRequirement = item.experienceRequirement.split(',');
            item.industryName = item.industryName.split(',');


          })
        }
        _this.setData({
          searchResultArr: res.data,
          searchArr: value,
          show: true
        })
      })
    } else {
      let searchArr = _this.data.searchArr;
      searchArr = wx.getStorageSync('searchArr')
      // console.log(searchArr)
      this.setData({
        searchArr
      })
      _this.setData({
        show: false
      })
    }
  },
  searchHistory(e) {
    console.log('点击', e.currentTarget.dataset.val)
    let _this = this;
    let val = e.currentTarget.dataset.val;
    var value = wx.getStorageSync('searchArr') || [] //声明plates
    value.unshift(val) //添加
    let searchArr = [];
    value = value.reduce(function (x, y) {
      searchArr[y] ? '' : searchArr[y] = true && x.push(y);
      return x;
    }, []);
    wx.setStorageSync('searchArr', value) //同步数据存储
    wx.showLoading({
      title: '加载中',
    })
    apis.searchSearchQuery({
      searchWhere: val
    }).then(res => {

      for (var i = 0; i <= res.data.news.records.length - 1; i++) {
        res.data.news.records[i].label = res.data.news.records[i].label.split(",");
        // res.data.news.records[i].label = res.data.news.records[i].label.slice(0);
      }
      for (var i = 0; i <= res.data.recruitment.records.length - 1; i++) {
        res.data.recruitment.records[i].experienceRequirement = res.data.recruitment.records[i].experienceRequirement.split(",");
        res.data.recruitment.records[i].experienceRequirement = res.data.recruitment.records[i].experienceRequirement.slice(0);
        res.data.recruitment.records[i].industryName = res.data.recruitment.records[i].industryName.split(",");
        res.data.recruitment.records[i].industryName = res.data.recruitment.records[i].industryName.slice(0);
      }
      _this.setData({
        searchResultArr: res.data,
        searchWord: val,
        show: true
      })
      // console.log(this.data.searchResultArr)

      wx.hideLoading()
    })
    this.setData({
      searchWord: val,
      show: true
    })

  },
  clearHistory() {
    wx.removeStorageSync('searchArr')
    this.setData({
      searchArr: []
    })
  },
  goDetail(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/curriculum/pages/course/index?id=' + id,
    })
  },
  goCareer(e) {
    let id = e.currentTarget.dataset.id;
    let industryId = e.currentTarget.dataset.industryId;
    wx.navigateTo({
      url: '/home/pages/index_career_detail/index?id=' + id + '&industryId=' + industryId,
    })
  },
  goDNews(e) {
    let item = e.currentTarget.dataset.item;
    wx.setStorageSync('jumpUrl', item.shareUrl)
    // 其他
    wx.navigateTo({
      url: '/activity/pages/jumpUrl/index',
    })

  },
  swtichTab(e) {
    // console.log(e)
    let tab = e.currentTarget.dataset.tab;
    if (tab == 'curriuml') {
      this.setData({
        searchShow: 1,
        switchShow: true,
        show: false,
        searchMoreNo:1,
      })
      this.getsearchSearchCurriculum()
    } else if (tab == 'employment') {
      this.setData({
        searchShow: 2,
        switchShow: true,
        show: false,
        searchMoreNo:1,
      })
      this.getsearchSearchRecruitment()
    } else if (tab == 'news') {
      this.setData({
        searchShow: 3,
        switchShow: true,
        show: false,
        searchMoreNo:1,
      })
      this.getsearchSearchNews()
    }
  },
  // 全部资讯结果
  getsearchSearchNews() {
    apis.searchSearchNews({
      searchWhere: this.data.searchWord,
      pageNo: this.data.searchMoreNo,
      pageSize: 10
    }).then(res => {
      if (res.data.records) {
        res.data.records.map(item => {
          item.label = item.label.split(',');
          console.log(item.label)

        })
      }
      if(this.data.searchMoreNo==1){
        this.setData({
          searchMore: res.data.records,
          searchTotal:res.data.total
        })
      }else{
        this.setData({
          searchMore:this.data.searchMore.concat(res.data.records),
          searchTotal:res.data.total
        })
      }
    })
  },
  // 全部课程结果
  getsearchSearchCurriculum() {
    apis.searchSearchCurriculum({
      searchWhere: this.data.searchWord,
      pageNo: this.data.searchMoreNo,
      pageSize: 10
    }).then(res => {
      if(this.data.searchMoreNo==1){
        this.setData({
          searchMore: res.data.records,
          searchTotal:res.data.total
        })
      }else{
        this.setData({
          searchMore:this.data.searchMore.concat(res.data.records),
          searchTotal:res.data.total
        })
      }
    })
  },
  // 全部招聘结果
  getsearchSearchRecruitment() {
    apis.searchSearchRecruitment({
      searchWhere: this.data.searchWord,
      pageNo: this.data.searchMoreNo,
      pageSize: 10
    }).then(res => {
      if (res.data.records) {
        res.data.records.map(item => {
          item.experienceRequirement = item.experienceRequirement.split(',');
          item.industryName = item.industryName.split(',');


        })
      }
      if(this.data.searchMoreNo==1){
        this.setData({
          searchMore: res.data.records,
          searchTotal:res.data.total
        })
      }else{
        this.setData({
          searchMore:this.data.searchMore.concat(res.data.records),
          searchTotal:res.data.total
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(this.data.searchResultArr)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.searchTotal>this.data.searchMore.length){
      this.setData({
        searchMoreNo: ++this.data.searchMoreNo
      })
      if(this.data.searchShow==1){
        this.getsearchSearchCurriculum()
      }else if(this.data.searchShow==2){
        this.getsearchSearchRecruitment()
      }else if(this.data.searchShow==3){
        this.getsearchSearchNews()
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})